/**
 * Created by Harrie on 2015/10/17.
 */

function indexOf(arr, condition) {
    for (var i = 0, len = arr.length; i < len; i++) {
        if (condition(i, arr[i])) return i;
    }
    return -1;
}

function removeFrom(arr, condition) {
    var i = indexOf(arr, condition);
    if (i >= 0)arr.splice(i, 1);
}

function generateId() {
    return +(new Date().getTime() + '' + (Math.random() * 1000).toFixed(3));
}

function format(str, obj) {
    var args = arguments;
    var result = ''
    if (args.length == 1)
        result = str;
    else if (args.length == 2 && typeof (obj) == "object") {
        for (var key in obj) {
            if (obj[key] != undefined) {
                var reg = new RegExp("({" + key + "})", "g");
                result = str.replace(reg, obj[key]);
            }
        }
    } else if (args.length >= 2) {
        for (var i = 1; i < args.length; i++) {
            if (args[i] != undefined) {
                var reg = new RegExp("({)" + (i - 1) + "(})", "g");
                result = str.replace(reg, args[i]);
            }
        }
    }
    return result;
}

module.exports = {
    indexOf: indexOf,
    removeFrom: removeFrom,
    generateId: generateId,
    format: format
}